package net.sapfree.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class LoginFilter implements Filter {

	protected FilterConfig config;
	public void init(FilterConfig filterconfig) throws ServletException{
		config = filterconfig;
	}
	
	public void doFilter(ServletRequest request,ServletResponse response,FilterChain filterChain) throws IOException,ServletException
	{
		System.out.println("---------in filter---------");
		HttpSession session = (HttpSession)((HttpServletRequest)request).getSession(false);
		//System.out.println("user id is "+session.getAttribute("userid"));
		if(session==null || session.getAttribute("userid")==null || session.getAttribute("userid").toString().equals("")){
			//System.out.println("relogin page ");
			config.getServletContext().getRequestDispatcher("/index.html").forward(request,response);
			
		}else{
			//System.out.println("url is "+((HttpServletRequest)request).getServletPath());
			config.getServletContext().getRequestDispatcher(((HttpServletRequest)request).getServletPath()).forward(request,response);
		}
		
	}
	public void destroy(){
	}

}
